﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using qltscd.Models;
using System.Web.Mvc;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using qltscd.Helper;
using System.Web.Routing;

namespace qltscd.Controllers
{
    public class BaoCaoController : BaseController
    {
        protected override void OnActionExecuting (ActionExecutingContext filterContext) {
            base.OnActionExecuting(filterContext);
            if (SessionHelper.QuyenNhom[qltscd.Contansts.KeyContansts.QuyenBaoCao] == '0') {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new {
                    controller = "KhongCoQuyen",
                    action = "Index"
                }));
            }
        }
        private QLTBEntities db = new QLTBEntities();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SoTheoDoiQuanLyThietBiTinHoc () {
            using (ReportClass rptH = new ReportClass()) {

                rptH.FileName = Server.MapPath("~") + "\\Reports\\SoTheoDoiQuanLyThietBi.rpt";
                rptH.SetDatabaseLogon("sa", "sa");
                rptH.SetParameterValue("paraTenDonVi", SessionHelper.DonViQuanLy);
                rptH.Load();
                //rptH.SetDatabaseLogon("huynh", "1234567890", "johnny-pc\\sqlexpress", "DuLieuQuanLyThietBi");
                //rptH.ExportToHttpResponse(ExportFormatType.PortableDocFormat, System.Web.HttpContext.Current.Response, false, "crReport");
                Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
                return File(stream, "application/pdf");
            }
        }

        public ActionResult SoTheoDoi() {

            var list = db.DanhMucThietBis.OrderBy(i => i.TheThietBi).ToList();
            return View(list);
        }
    }
}
